var app = angular.module('stockgroupDetail', []);
// var $tab = $('#doc-my-tabs');
var view = echarts.init(document.getElementById('barView'), macarons);


app.controller('detailController', function ($scope,$http) {
    // var myTabs = angular.element('#my-tabs');
    $scope.page = 'default';
    $scope.groupId = groupId;
    $scope.thisUserId = thisUserId;
    $scope.group = {};
    // $scope.groupUserId = groupUserId;
    $scope.dealList = [];
    $scope.user = {};
    $scope.stockList = [];
    $scope.stockList = [];
    $scope.default = [];
    $scope.showChart = true;
    $scope.url = "../stockgroup/detail";
    getData();
    $scope.changePage = function(page){
        $scope.page = page;

        if($scope.page == 'stockList'){
            $scope.url = "../stockgroup/stockList";
        }else if($scope.page == 'dealList'){
            $scope.url = "../stockdeal/dealList"
        }else{
            $scope.url = "../stockgroup/detail";
        }
        getData();
    };
    function getData(){
        if(!$scope.groupId)return;
        $http.post($scope.url,{
            params: {
                groupId:$scope.groupId,
                //groupUserId:$scope.groupUserId
            }
        }).then(function (result) {
            console.log(result)
            if($scope.page == 'stockList'||($scope.page == 'dealList')){
                $scope[$scope.page].splice(0,$scope[$scope.page].length);
                for(item of result.data){
                    $scope[$scope.page].push(item);
                }
            }else if($scope.page == 'default'){
                $scope.group = result.data.result;
                $scope.user = result.data.result.createUid;
                var yDates = [];
                var xDates = [];
                for(index in result.data.result.stockGroupRates){
                    yDates.push(result.data.result.stockGroupRates[index].rate);
                    xDates.push(result.data.result.stockGroupRates[index].countDate)
                }
                if(yDates.length == 0 || xDates.length == 0 ){
                    $scope.showChart = false;
                }else{
                    $scope.showChart = true;
                    initChar(xDates,yDates);
                }

            }

        });
    }
    $scope.toDetail = function(rowId) {
        location.href="../stockgroup/detail?rowId="+rowId;
    };
    $scope.buy = function(){
        location.href = "../stockdeal/edit?type=1&groupId="+$scope.groupId;
    }
    $scope.editStockPrice = function(stock){
        $scope.stock = stock;
        var positionNum = stock.positionNum;
        var cost = stock.cost;
        $('#my-prompt').modal({
            relatedTarget: this,
            onConfirm: function(e) {
                if(!stock.rowId||(!e.data[0]&&!e.data[1])||e.data[1]<positionNum){
                    $scope.stock.positionNum = positionNum;
                    $scope.cost = cost;
                    return;
                }
                $http.post("../stockChannel/update",{
                    params: {
                        cost: e.data[0],
                        positionNum: e.data[1],
                        rowId: stock.rowId,
                    }
                }).then(function (result) {
                   console.log(result);
                });

            },
            onCancel: function(e) {

            }
        });
    }
    $scope.sell = function(){
        location.href = "../stockdeal/edit?type=0&groupId="+$scope.groupId;
    }

    function initChar(xDates,yDates){
        option = {
            title: {
                text: '收益持仓走势',
                subtext: '',
                left: 'center'
            },
            tooltip: {
                trigger: 'axis',
                formatter: "{a}:{c}%"
            },
            legend: {
                data: [
                    //'组合仓位',
                    '组合收益'
                ]
            },

            calculable: true,
            xAxis: [
                {
                    type: 'category',
                    boundaryGap: false,
                    data: xDates
                        //['周一', '周二', '周三', '周四', '周五', '周六', '周日']
                }
            ],
            yAxis: [
                {
                    type: 'value',
                    axisLabel: {
                        formatter: '{value} '
                    }
                }
            ],
            series: [
                {
                    name: '收益',
                    type: 'line',
                    data: yDates,
                    //[11, 11, 19, 12, 12, 11, 10],
                    //label: { show: true,formatter: '{value}%'}  ,
                    label: {
                        normal: {
                            show: true,
                            position: 'outside',
                            formatter: '{c}%' // 这里是数据展示的时候显示的数据
                        }
                    },
                    markPoint: {
                        data: [
                            {type: 'max', name: '最大值'},
                            {type: 'min', name: '最小值'}
                        ]
                    },
                    markLine: {
                        data: [
                            {type: 'average', name: '平均值'}
                        ]
                    }
                },
                // {
                //     name: '最低气温',
                //     type: 'line',
                //     data: [1, -2, 2, 5, 3, 2, 0],
                //     markPoint: {
                //         data: [
                //             {name: '周最低', value: -2, xAxis: 1, yAxis: -1.5}
                //         ]
                //     },
                //     markLine: {
                //         data: [
                //             {type: 'average', name: '平均值'}
                //         ]
                //     }
                // }
            ]
        };
        view.setOption(option);
    }
});
app.filter('roundNum',function() {
    return function (val,price,positionPrice) {
        return (val*price/positionPrice*100).toFixed(2);
    }
});
app.filter('cutNum',function() {
    return function (val) {
        if(val <10000) return val;
        val = Math.round((val /10000) * 100) / 100;
        return val + "万";

    }
});
